$(function(){
    var key = getCookie('key');
    if (key) {
        window.location.href = WapSiteUrl+'/tmpl/member/member.html';
        return;
    }
    $.getJSON(ApiUrl + '/index.php?act=connect&op=get_state&t=connect_sms_reg', function(result){
        if (result.datas != '0') {
            $('.register-tab').show();
        }
    });
    
	$.sValid.init({//注册验证
        rules:{
        	username:"required",
            userpwd:"required",            
            password_confirm:"required",
            area_info:"required",
            email:{
            	required:true,
            	email:true
            }
        },
        messages:{
            username:"用户名必须填写！",
            userpwd:"密码必填!", 
            password_confirm:"确认密码必填!",
            area_info:"地区必填！",
            email:{
            	required:"邮件必填!",
            	email:"邮件格式不正确"
            }
        },
        callback:function (eId,eMsg,eRules){
            if(eId.length >0){
                var errorHtml = "";
                $.map(eMsg,function (idx,item){
                    errorHtml += "<p>"+idx+"</p>";
                });
                errorTipsShow(errorHtml);
            }else{
                errorTipsHide();
            }
        }  
    });
	
	$('#registerbtn').click(function(){
        if (!$(this).parent().hasClass('ok')) {
            return false;
        }
		var username = $("input[name=username]").val();
		var pwd = $("input[name=pwd]").val();
		var password_confirm = $("input[name=password_confirm]").val();
        var address = $('#address').val();
        var province_id = $('#area_info').attr('data-areaid1');
        var city_id = $('#area_info').attr('data-areaid2');
        var area_id = $('#area_info').attr('data-areaid');
        var area_info = $('#area_info').val();
		var email = $("input[name=email]").val();
		var client = 'wap';
		
		if($.sValid()){
			$.ajax({
				type:'post',
				url:ApiUrl+"/index.php?act=login&op=register",	
				data:{
                    username:username,
                    password:pwd,
                    password_confirm:password_confirm,
                    email:email,
                    client:client,
                    province_id:province_id,
                    city_id:city_id,
                    area_id:area_id,
                    address:address,
                    area_info:area_info,
                },
				dataType:'json',
				success:function(result){
					if(!result.datas.error){
						if(typeof(result.datas.key)=='undefined'){
							return false;
						}else{
                            // 更新cookie购物车
                            updateCookieCart(result.datas.key);
							addCookie('username',result.datas.username);
							addCookie('key',result.datas.key);
							location.href = WapSiteUrl+'/tmpl/member/member.html';
						}
		                errorTipsHide();
					}else{
		                errorTipsShow("<p>"+result.datas.error+"</p>");
					}
				}
			});			
		}
	});

    // 选择地区
    $('#area_info').on('click', function(){
        $.areaSelected({
            success : function(data){
                console.log(data);
                $('#area_info').val(data.area_info).attr({'data-areaid':data.area_id,'data-areaid1':data.area_id_1 , 'data-areaid2':(data.area_id_2 == 0 ? data.area_id_1 : data.area_id_2),'data-areaid3':(data.area_id_3 == 0 ? data.area_id_2 : data.area_id_3)});
            }
        });
    });

    //их└ч
    var uid = window.location.href.split("#V5");
    var  fragment = uid[1];
    if(fragment){
        if (fragment.indexOf("V5") == 0) {
            addCookie("uid", "0");
        }else {
            addCookie("uid", fragment);
        }
    }
});


